package com.shen.car;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Author:shen
 * CreateDate:2020/5/10.
 * Function:
 */
public class CarManager {
    static Map<Integer,Car> carMap=new LinkedHashMap<>();
    static int carId=4;
    static {
        carMap.put(1,new Car("皖A97302",true,0,13,190));
        carMap.put(2,new Car("皖B4932",false,2,9,170));
        carMap.put(3,new Car("皖A492F",true,0,3,160));
        carMap.put(4,new Car("皖A9014",true,0,5,165));
    }
    /**
     * 查询车牌号
     */
    public int checkName(String name){
      int index=-1;
        for (Map.Entry<Integer,Car> entry:carMap.entrySet()) {
            if(entry.getValue().getName().equals(name)){
              index=entry.getKey();
            }
        }
        return index;
    }
    /**
     * 2.查看车辆信息
     */
    public void queryAllCars(){
        System.out.println("序号\t车辆信息\t状态\t借出日期\t借出次数\t日租金");
        for (Map.Entry<Integer,Car> entry:carMap.entrySet()) {
            System.out.print(entry.getKey()+"\t");
            Car car=entry.getValue();
            String carState=car.getState()==true?"出库":"借出";
            System.out.print(
                    car.getName()+"\t"
                    +carState+"\t\t"
                    +car.getDate()+"\t\t"
                    +car.getCount()+"\t"
                    +car.getMoney()+"\n"
            );
        }
    }
    /**
     * 新增车辆
     */
    public void addCar(String name,int money){
        if(carId==6){
            System.out.println("停车位已满！");
        }else{
            if(checkName(name)!=-1){
                System.out.println("车牌号重复！");
            }else{
                Car car=new Car(name,true,0,0,money);
                carMap.put(carId++,car);
                System.out.println("增加成功！");
            }
        }
    }
    /**
     * 删除淘汰车
     */
    public void deleteCar(String name){
        int key=checkName(name);
        if(key!=-1){
            if (carMap.get(key).getState() == true) {
                carMap.remove(key);
                System.out.println("淘汰成功");
            }else{
                System.out.println("该车已借出！");
            }
        }else{
            System.out.println("车牌号不存在");
        }
    }
    /**
     * 借出车
     */
    public void lendCar(String name,int date){
       int key=checkName(name);
        if(key==-1){
            System.out.println("没有该车");
        }else{
            Car car=carMap.get(key);
            if(car.getState()==false){
                System.out.println("该车已被借走");
            }else{
               car.setState(false);
               car.setDate(date);
               car.setCount(car.getCount()+1);
                System.out.println("借出成功！");
            }
        }
    }
    /**
     * 车辆归还
     */
    public void  returnCar(String name){
        int key=checkName(name);
        if(key==-1){
            System.out.println("没有该车");
        }else{
            Car car=carMap.get(key);
            if(car.getState()==true){
                System.out.println("该车未租出");
            }else{
                System.out.println("租金为"+car.getDate()*car.getMoney());
                car.setState(true);
                car.setDate(0);
                System.out.println("归还成功！");
            }
        }
    }
}
